package com.wdy.action;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.wdy.po.Building;
import com.wdy.service.BuildingManager;
import com.wdy.vo.Condition;
import com.wdy.vo.Page;
import com.wdy.vo.Result;

@SuppressWarnings( { "serial", "unused" })
public class BuildingAction extends BasicAction {
	private static final Log log = LogFactory.getLog(BuildingAction.class);
	private Building building;
	private BuildingManager buildingManager;
	private Page page;
	private List<Condition> conList;
	private String[] ids;
	private Map<String, Object> session;
	private HttpServletResponse response;
	private HttpServletRequest request;
	private ServletContext servletContext;

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public Building getBuilding() {
		return building;
	}

	public void setBuilding(Building building) {
		this.building = building;
	}

	public void setBuildingManager(BuildingManager buildingManager) {
		this.buildingManager = buildingManager;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<Condition> getConList() {
		return conList;
	}

	public void setConList(List<Condition> conList) {
		this.conList = conList;
	}
	
	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	// building list.action
	@SkipValidation
	public String listAll() {
		List<Building> list = null;
		list = (List<Building>) buildingManager.listAll();
		if (null != list) {
			request.setAttribute("buildingList", list);
		}
		return SUCCESS;
	}

	// building add.action
	public String add() {
		buildingManager.saveBuilding(building);
		return SUCCESS;
	}

	// building delete.action
	@SkipValidation
	public String delete() {
		buildingManager.removeBuilding(building);
		return SUCCESS;
	}

	// building update.action
	public String update() {
		buildingManager.updateBuilding(building);
		return SUCCESS;
	}

	// building findByExample.action
	@SkipValidation
	public String find() {
		List<Building> list = null;
		list = (List<Building>) buildingManager.findByExample(building);
		if (null != list) {
			request.setAttribute("buildings", list);
		}
		return SUCCESS;
	}

	// building get.action
	@SkipValidation
	public String get() {
		Building building = null;
		building = (Building) buildingManager.getBuilding(this.building
				.getBid());
		if (null != building) {
			request.setAttribute("building", building);
		}
		return SUCCESS;
	}

	// building findByAddress.action
	@SkipValidation
	public String findByAddress() {
		List<Building> list = null;
		list = (List<Building>) buildingManager.findByAddress(building
				.getAddress());
		if (null != list) {
			request.setAttribute("buildings", list);
		}
		return SUCCESS;
	}

	// building query_building.action
	@SkipValidation
	public String query()throws Exception{
//		conList = (List<Condition>)session.get("conList");
		if(null == conList || conList.size() == 0){
			conList = new LinkedList<Condition>();
		}
		Iterator<Condition> it = conList.iterator();
		while(it.hasNext()){
			Condition con = (Condition)it.next();
			if(null != con.getValues()){
				Object[] values = con.getValues();
				if(null == values[0] || "".equals(values[0])){
					it.remove();
				}
			}else{
				it.remove();
			}
		}
		
		if(null == page){
			page = new Page();
			page.setEveryPage(5);
			page.setCurrentPage(1);
		}
		Result result = buildingManager.query(conList, page);
		request.setAttribute("page", result.getPage());
		request.setAttribute("buildingList", result.getContent());
//		session.put("conList", conList);
		return SUCCESS;
	}
	
	// building delete_batch.action
	@SkipValidation
	public String deleteBatch()throws Exception{
		if(null != ids){
			Integer[] id = new Integer[ids.length];
			for(int i = 0; i < ids.length; i++){
				id[i] = new Integer(ids[i]);
			}
			buildingManager.removeBuildings(id);
		}
		return SUCCESS;
	}
	
}
